package com.whmall.service.crm.module.company.service.impl;

import cn.hutool.json.JSONUtil;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.company.CompanyAndUserApproveKeyFieldRspDTO;
import com.whmall.service.crm.entity.dto.company.CompanyAndUserApproveListReqDTO;
import com.whmall.service.crm.entity.dto.company.CompanyAndUserApproveListRspDTO;
import com.whmall.service.crm.entity.dto.companyuser.CompanyUserNewUserApplyDTO;
import com.whmall.service.crm.module.company.dao.*;
import com.whmall.service.crm.module.company.entity.CompanyAndUserApplicationInformationDO;
import com.whmall.service.crm.module.company.entity.CompanyAndUserApproveDO;
import com.whmall.service.crm.module.company.entity.CompanyAndUserApproveInformationDO;
import com.whmall.service.crm.module.company.entity.CompanyDO;
import com.whmall.service.crm.module.company.entity.qo.CompanyAndUserApplicationInformationQO;
import com.whmall.service.crm.module.company.entity.qo.CompanyAndUserApproveInformationQO;
import com.whmall.service.crm.module.company.entity.qo.CompanyAndUserApproveQO;
import com.whmall.service.crm.module.company.service.CompanyAndUserApproveService;
import com.whmall.service.crm.util.PageDTOHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CompanyAndUserApproveServiceImpl implements CompanyAndUserApproveService {

    @Autowired
    private CompanyAndUserApplicationMapper companyAndUserApplicationMapper;
    @Autowired
    private CompanyAndUserApplicationInformationMapper companyAndUserApplicationInfoMapper;
    @Autowired
    private CompanyAndUserApproveMapper companyAndUserApproveMapper;
    @Autowired
    private CompanyAndUserApproveInformationMapper companyAndUserApproveInfoMapper;
    @Autowired
    private CompanyMapper companyMapper;

    @SuppressWarnings("unchecked")
    @Override
    public PageRspDTO<CompanyAndUserApproveListRspDTO> list(PageReqDTO pageReqDTO, CompanyAndUserApproveListReqDTO reqDTO) {
        CompanyAndUserApproveQO qo = new CompanyAndUserApproveQO();
        BeanUtils.copyProperties(reqDTO, qo);
        qo.setIsDeleted(0);
        qo.setIsCancel(0);
        qo.setOrderBy("id desc");
        PageDTOHelper.startPage(pageReqDTO);
        List<CompanyAndUserApproveDO> approves = companyAndUserApproveMapper.selectBy(qo);
        PageRspDTO pageRspDTO = PageDTOHelper.toPageDTO(approves);
        if(approves.isEmpty()){
            return pageRspDTO;
        }
        Map<Long, CompanyAndUserApproveInformationDO> approveInfos = companyAndUserApproveInfoMapper.selectBy(CompanyAndUserApproveInformationQO.builder()
                        .ids(approves.stream().map(e -> e.getId()).collect(Collectors.toList()))
                        .build())
                        .stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
        Map<Long, CompanyAndUserApplicationInformationDO> applicationInformationMap = companyAndUserApplicationInfoMapper.selectBy(CompanyAndUserApplicationInformationQO.builder()
                .ids(approves.stream()
                        .map(CompanyAndUserApproveDO::getApplicationId)
                        .distinct()
                        .collect(Collectors.toList()))
                .build()).stream().collect(Collectors.toMap(CompanyAndUserApplicationInformationDO::getId, e -> e));

        List<CompanyDO> companyList = companyMapper.getByIds(approves.stream().map(CompanyAndUserApproveDO::getCompanyId)
                .distinct()
                .collect(Collectors.toList()));

        List<CompanyAndUserApproveListRspDTO> rspDTOs = approves.stream().map(e -> {
            CompanyAndUserApplicationInformationDO applicationInformationDO = applicationInformationMap.get(e.getApplicationId());
            CompanyAndUserApproveListRspDTO rspDTO = new CompanyAndUserApproveListRspDTO();
            BeanUtils.copyProperties(e, rspDTO);
            BeanUtils.copyProperties(approveInfos.get(e.getId()), rspDTO);
            companyList.stream()
                    .filter(c -> c.getId().equals(e.getCompanyId()))
                    .findFirst()
                    .ifPresent(c -> rspDTO.setCompanyName(c.getName()));
            return rspDTO;
        }).collect(Collectors.toList());
        pageRspDTO.setRecords(rspDTOs);
        return pageRspDTO;
    }

    @Override
    public Map<Long, CompanyAndUserApproveKeyFieldRspDTO> selectByIds(List<Long> ids) {
        if(CollectionUtils.isEmpty(ids)){
            return Collections.emptyMap();
        }
        List<CompanyAndUserApproveDO> approves = companyAndUserApproveMapper.selectBy(
                CompanyAndUserApproveQO.builder().ids(ids).build());
        Map<Long, CompanyAndUserApproveInformationDO> infos = companyAndUserApproveInfoMapper.selectBy(
                CompanyAndUserApproveInformationQO.builder().ids(ids).build())
                .stream().collect(Collectors.toMap(e -> e.getId(), e -> e));
        return approves.stream().collect(Collectors.toMap(e -> e.getId(), e -> {
            CompanyAndUserApproveKeyFieldRspDTO rspDTO = new CompanyAndUserApproveKeyFieldRspDTO();
            BeanUtils.copyProperties(e, rspDTO);
            BeanUtils.copyProperties(infos.get(e.getId()), rspDTO);
            return rspDTO;
        }));
    }

    @Override
    public List<Map<String, Object>> getCountByVerifyId(Long verifyId) {
        return companyAndUserApproveMapper.selectCountByVerifyId(verifyId);
    }

}
